package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Tag(name = "声音管理")
@RestController
@RequestMapping("/api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	private static final Logger log = LoggerFactory.getLogger(TrackInfoApiController.class);
	@Autowired
	private TrackInfoService trackInfoService;

	@Operation(summary = "声音上传")
	@PostMapping("/uploadTrack")
	public Result uploadTrack(MultipartFile file) {
		Map<String, Object> map = trackInfoService.uploadTrack(file);
		return Result.ok(map);
	}

	/**
	 * 保存声音
	 * @return
	 */
	@Operation(summary = "保存声音")
	@PostMapping("/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody @Validated TrackInfoVo trackInfoVo){
		log.info("保存声音");
		trackInfoService.saveTrackInfo(trackInfoVo);
		return Result.ok();
	}

	@Operation(summary = "查看声音列表")
	@PostMapping("/findUserTrackPage/{page}/{limit}")
	public Result findUserTrackPage(@PathVariable Long page,
				@PathVariable Long limit, @RequestBody TrackInfoQuery trackInfoQuery){
		log.info("查看声音列表:{},{}",page,limit);
		Page<TrackListVo> trackListVoPage = new Page<>(page, limit);
		IPage<TrackListVo> trackListVoIPage = trackInfoService.findUserTrackPage(trackListVoPage,trackInfoQuery);
		return Result.ok(trackListVoIPage);
	}

	/**
	 * 删除声音
	 * @param trackId
	 * @return
	 */
	@Operation(summary = "删除声音")
	@DeleteMapping("/removeTrackInfo/{trackId}")
	public Result removeTrackInfo(@PathVariable Long trackId){
		log.info("删除声音{}",trackId);
		trackInfoService.removeTrackInfoById(trackId);
		return Result.ok();
	}

	/**
	 * 根据声音id获取回显数据
	 * @param trackId
	 * @return
	 */
	@Operation(summary = "根据声音id获取回显数据")
	@GetMapping("/getTrackInfo/{trackId}")
	public Result getTrackInfoById(@PathVariable Long trackId){
		log.info("根据声音id获取回显数据:{}",trackId);
		TrackInfo trackInfo = trackInfoService.getTrackInfoById(trackId);
		return Result.ok(trackInfo);
	}

	/**
	 * 修改声音
	 * @param trackId
	 * @param trackInfoVo
	 * @return
	 */
	@Operation(summary = "修改声音")
	@PutMapping("/updateTrackInfo/{trackId}")
	public Result updateTrackInfo(@PathVariable Long trackId, @RequestBody @Validated TrackInfoVo trackInfoVo){
		log.info("修改声音:{},{}",trackId,trackInfoVo);
		trackInfoService.updateTrackInfo(trackId,trackInfoVo);
		return Result.ok();
	}
}

